package com.example.auction.service.impl;

import com.example.auction.context.BaseContext;
import com.example.auction.entity.Goods;
import com.example.auction.entity.Item;
import com.example.auction.entity.dto.GoodsDTO;
import com.example.auction.entity.dto.ItemDTO;
import com.example.auction.entity.vo.GoodsVO;
import com.example.auction.mapper.GoodsMapper;
import com.example.auction.mapper.ItemMapper;
import com.example.auction.mapper.UserMapper;
import com.example.auction.service.GoodsService;
import com.example.auction.service.ItemService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;
    /*
    * 新建商品接口
    * */
    @Override
    public void createGoods(GoodsDTO goodsDTO) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO, goods);
        goods.setCreateTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goods.setCreateId(BaseContext.getCurrentId());
        goods.setUpdateId(BaseContext.getCurrentId());
        goodsMapper.insert(goods);
    }

    /*
    * 根据id查询商品列表
    * */
    @Override
    public List<GoodsVO> selectById(Long id) {
        List<GoodsVO> goods = new ArrayList<>();
        goods = goodsMapper.selectByIdList(id);
        /*
        * 根据用户查询商品需要根据用户id联查goods(根据分类查询商品,前提是这个商品包含在分类内,所以直接调用查询菜品根据分类即可即可)
        * 根据当前id查询商品
        * 要保证查询到的商品是该用户下的商品 (为商品创建用户外键)
        * 数据库的查询需要返回集合 数据库左连接查询(select * from goods left join user on user_id = #{userId})
        * */
        /*SELECT
        u.id,
                u.name,
                g.user_id
        FROM user u
        LEFT JOIN goods g ON u.id = g.user_id;*/
        return null;
    }
}
